package com.cloudkinto.controller.finance;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.extentity.track.OrderTrackNumberDto;
import com.cloudkinto.extentity.track.OrderTrackUnMatchDto;
import com.cloudkinto.service.finance.vo.track.MatchTrackBillReq;
import com.cloudkinto.service.finance.vo.track.OrderTrackBillRes;
import com.cloudkinto.service.finance.vo.track.UpdateAmountReq;
import com.cloudkinto.service.finance.vo.track.UpdateTrackCompanyReq;
import com.cloudkinto.service.product.vo.ProductOutExportReq;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.finance.OrderTrackBillService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author hua
 * @since 2023-08-02
 */
@RestController
@Api(tags = "快递运费账单模块")
@RequestMapping("/api/admin/OrderTrackBill")
public class OrderTrackBillController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrderTrackBillController.class);

    @Autowired
    private OrderTrackBillService service;

//    @GetMapping("/addInit")
//    @ApiOperation(value = "新增初始化", notes = "新增初始化")
//    public SingleResult addInit() {
//        return service.addInit();
//    }
//
//    @PostMapping("")
//    @ApiOperation(value = "新增", notes = "新增")
//    public SingleResult add(@RequestBody @Validated Object req) {
//        Long userId = getCurrentUserId();
//        Long companyId = getCompanyId();
//        return SingleResult.success("新增成功",service.add(req,userId));
//    }

//    @GetMapping("/updateInit/{id}")
//    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
//    public SingleResult updateInit(@PathVariable("id") Long id) {
//        return service.updateInit(id);
//    }

//    @PutMapping("")
//    @ApiOperation(value = "修改", notes = "修改")
//    public SingleResult update(@RequestBody @Validated Object req) {
//        Long userId = getCurrentUserId();
//        return SingleResult.success("修改成功",service.update(req,userId));
//    }

//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "根据id删除", notes = "根据id删除")
//    public SingleResult delete(@PathVariable("id") Long id) {
//        Long userId = getCurrentUserId();
//        return SingleResult.success("删除成功",service.delete(id,userId));
//    }

//    @GetMapping("/{id}")
//    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
//    public SingleResult detail(@PathVariable("id") Long id) {
//        return SingleResult.success("查询成功",service.detail(id));
//    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

//    @GetMapping("/list")
//    @ApiOperation(value = "查询", notes = "查询")
//    public SingleResult list(HttpServletRequest request){
//        Long companyId = getCompanyId();
//
//        Map<String, String> map = new HashMap<>();
//        String keyword = request.getParameter("keyword");
//        map.put("keyword", keyword);
//        return SingleResult.success("查询成功", service.getList(map));
//    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "current"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize"),
//            @ApiImplicitParam(name = "keyword", value = "关键词"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "trackId", value = "快递公司"),
            @ApiImplicitParam(name = "trackMatch", value = "匹配状态 0 未匹配 1已匹配"),
            @ApiImplicitParam(name = "matchResult", value = "匹配结果 normal 正常 more 物流多收 less 物流少收"),
            @ApiImplicitParam(name = "packageType", value = "包裹类型 1 一单一件 2 一单多件"),
            @ApiImplicitParam(name = "beginTime", value = "发货时间 开始时间"),
            @ApiImplicitParam(name = "endTime", value = "发货时间 结束时间"),
            @ApiImplicitParam(name = "orderNo", value = "出库单号"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列 利润 profit"),
            @ApiImplicitParam(name = "sort", value = "排序方式 倒叙 desc 正序 asc"),
            @ApiImplicitParam(name = "profitType", value = "利润计算方式 0 不含其他费用 1 含其他费用 默认选择 0")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OrderTrackBillRes.class))
    public PageResult listPage(HttpServletRequest request){

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        String storageId = request.getParameter("storageId");
        String trackId = request.getParameter("trackId");
        String trackMatch = request.getParameter("trackMatch");
        String matchResult = request.getParameter("matchResult");
        String packageType = request.getParameter("packageType");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        String orderNo = request.getParameter("orderNo");
        String sortColumn = request.getParameter("sortColumn");
        String sort = request.getParameter("sort");
        String profitType = request.getParameter("profitType");


        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("storageId", storageId);
        map.put("trackId", trackId);
        map.put("trackMatch", trackMatch);
        map.put("matchResult", matchResult);
        map.put("packageType", packageType);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("orderNo", orderNo);
        map.put("sortColumn", sortColumn);
        map.put("sort", sort);
        map.put("profitType", profitType);

        return service.getListPage(map);
    }


    @UserSessionRequired
    @PostMapping("/importTrackBill")
    @ApiOperation(value = "导入Excel")
    public SingleResult importOrderOut(MultipartFile file, String year)  throws IOException {
        service.importTrackBill(file, year, getCurrentUserId());
        return SingleResult.success("导入成功");
    }


    @UserSessionRequired
    @PutMapping("/updateTrackCompany")
    @ApiOperation(value = "修改快递公司", notes = "修改快递公司")
    public SingleResult updateTrackCompany(@RequestBody @Validated UpdateTrackCompanyReq req) {
        service.updateTrackCompany(req);
        return SingleResult.success("修改成功");
    }


    @UserSessionRequired
    @GetMapping("/getUnMatchList")
    @ApiOperation(value = "匹配账单-获取未匹配的账单", notes = "匹配账单-获取未匹配的账单")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OrderTrackUnMatchDto.class))
    public PageResult getUnMatchList(String beginTime, String endTime, String keyword, String current, String pageSize) {

        Map<String, String> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", pageSize);
        return service.getUnMatchList(map);
    }

    @UserSessionRequired
    @PutMapping("/matchTrackBill")
    @ApiOperation(value = "匹配账单", notes = "匹配账单")
    public SingleResult matchTrackBill(@RequestBody @Validated MatchTrackBillReq req) {
        service.matchTrackBill(req, getCurrentUserId());
        return SingleResult.success();
    }

    @UserSessionRequired
    @GetMapping("/getTrackInfo")
    @ApiOperation(value = "修改金额初始化", notes = "修改金额初始化")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OrderTrackNumberDto.class))
    public SingleResult getTrackInfo(Long id, String orderType) {
        return SingleResult.success(service.getTrackInfo(id, orderType));
    }


    @UserSessionRequired
    @PutMapping("/updateAmount")
    @ApiOperation(value = "修改金额", notes = "修改金额")
    public SingleResult updateAmount(@RequestBody @Validated UpdateAmountReq req) {
        service.updateAmount(req, getCurrentUserId());
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @GetMapping("export")
    @ApiOperation(value = "导出数据")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "keyword", value = "关键词"),
            @ApiImplicitParam(name = "storageId", value = "仓库id"),
            @ApiImplicitParam(name = "trackId", value = "快递公司"),
            @ApiImplicitParam(name = "trackMatch", value = "匹配状态 0 未匹配 1已匹配"),
            @ApiImplicitParam(name = "matchResult", value = "匹配结果 normal 正常 more 物流多收 less 物流少收"),
            @ApiImplicitParam(name = "packageType", value = "包裹类型 1 一单一件 2 一单多件"),
            @ApiImplicitParam(name = "beginTime", value = "发货时间 开始时间"),
            @ApiImplicitParam(name = "endTime", value = "发货时间 结束时间"),
            @ApiImplicitParam(name = "orderNo", value = "出库单号"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列 利润 profit"),
            @ApiImplicitParam(name = "sort", value = "排序方式 倒叙 desc 正序 asc"),
            @ApiImplicitParam(name = "profitType", value = "利润计算方式 0 不含其他费用 1 含其他费用 默认选择 0")
    })
    public void export(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> map = new HashMap<>();

        String storageId = request.getParameter("storageId");
        String trackId = request.getParameter("trackId");
        String trackMatch = request.getParameter("trackMatch");
        String matchResult = request.getParameter("matchResult");
        String packageType = request.getParameter("packageType");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        String orderNo = request.getParameter("orderNo");
        String sortColumn = request.getParameter("sortColumn");
        String sort = request.getParameter("sort");
        String profitType = request.getParameter("profitType");
        map.put("storageId", storageId);
        map.put("trackId", trackId);
        map.put("trackMatch", trackMatch);
        map.put("matchResult", matchResult);
        map.put("packageType", packageType);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("orderNo", orderNo);
        map.put("sortColumn", sortColumn);
        map.put("sort", sort);
        map.put("profitType", profitType);
        service.export(map, response);
    }


    @UserSessionRequired
    @GetMapping("/markMatched")
    @ApiOperation(value = "标记已匹配", notes = "标记已匹配")
    public SingleResult markMatched(Long id, String orderType) {
        service.markMatched(id, orderType);
        return SingleResult.success();
    }

}

